/*******************************************************************************
 * @brief: systask
 * @Author: aple
 * @Date: 2022-12-04 10:37:03
 * @LastEditors: aple
 * @LastEditTime: 2022-12-07 19:27:41
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "mbedtls/sha256.h" 
#include "bsp_api.h"
#include "sys_task.h"
#include "pincfg.h"
#include "mytype.h"
#include "sys_init.h"
#include "framework.h"


/*****************************************************************************
 * @brief   var
 *****************************************************************************/
extern HASH_HandleTypeDef hhash;


/*******************************************************************************
* @brief  sha256_demo.
* \param[in] none
* \retval: none
*******************************************************************************/
int sha256_demo(int argc, char * argv[])
{
	uu08 * src , *out;
	uu32 clk_start;
	mbedtls_sha256_context *sha256;
	
	dbg_print("\nmbed SHA256 test :\n");
	
	// 请求内存
	src = malloc(nKB(8));
	out = calloc(1, 32);
	sha256 = malloc(sizeof(mbedtls_sha256_context));
	
	// 开始
	loop(nKB(8))
		src[index] = index;
	
	// 输入数据流，长度任意
	clk_start = bsp_cpu_clks_get();
	mbedtls_sha256_init(sha256);
	mbedtls_sha256_starts(sha256, 0); // \c 0 for SHA-256, or \c 1 for SHA-224	
	mbedtls_sha256_update(sha256, src, nKB(8));
	mbedtls_sha256_finish(sha256, out);
	mbedtls_sha256_free(sha256); 
	dbg_print("MBEDTLS sha256 time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
	
	// 显示
	dbg_u8_print("out : \n", out, 32, 16);

	memset(out, 0, 32);
		loop(nKB(8))
		src[index] = index;
	clk_start = bsp_cpu_clks_get();
	
	HAL_HASH_Start(&hhash, src, nKB(8),out,100000);
	dbg_print("\nSTM32 sha256 time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
	dbg_u8_print("out : \n", out, 32, 16);
	
	// 释放内存
	free(src);
	free(out);
	free(sha256);
	
	return 0;
}


DBG_FUN_EXPORT(sha256_demo);

